#pragma once
#pragma warning(disable:4002)
#if _DEBUG
#	pragma warning(disable:4127)
#endif
#include <cstdio>

#define NET_DEBUG 2

#ifndef _WIN32
#include <netinet/in.h>
#define sprintf_s sprintf
#define strncpy_s strncpy
#endif

extern int last_exit;
#define exit(x) (exit)(last_exit = (x))

#if _DEBUG
#define hex_dump(ptr, len) \
	{ for(int i = 0; i < (int)((len)/16); i++) { \
			printf("%p:", ptr+i*16); \
			for(int j = 0; j < 16; j++) \
				printf(" %.2x", ptr[i*16+j]);\
			printf(" | "); \
			for(int j = 0; j < 16; j++) \
				printf("%c", ptr[i*16+j] < 32 ? '.' : ptr[i*16+j]);\
			putchar('\n');\
		} \
		if( (int)((len)%16) != 0 ) { \
			printf("%p:", ptr+(int)((len)/16)*16); \
			for(int j = 0; j < (int)((len)%16); j++) \
				printf(" %.2x", ptr[(int)((len)/16)*16+j]);\
			for(int j = (int)((len)%16); j < 16; j++) \
				printf(" XX");\
			printf(" | "); \
			for(int j = 0; j < (int)((len)%16); j++) \
				printf("%c", ptr[(int)((len)/16)*16+j] < 32 ? '.' : ptr[(int)((len)/16)*16+j]);\
			putchar('\n');\
		}\
	}
#else
#define hex_dump(ptr, len) (void)0
#endif

#define _STR(a) #a
#define ASSERT( x ) \
	if(!x) { \
		fprintf(stderr, "error: ASSERTION FAILED in " _STR(__FILE__) " at line " _STR(__LINE__) ".\n");\
		exit(3);\
	}

#define LOG( x, ... )		{ fprintf(stderr, "[ LOG ] "); fprintf(stderr, x, ##__VA_ARGS__ ); }
#define ERRORLOG( x, ... )  { fprintf(stderr, "[ERROR] "); fprintf(stderr, x, ##__VA_ARGS__ ); }
#ifdef _DEBUG
#	define LOG2( x, ... )	{ fprintf(stderr, "[DEBUG] "); fprintf(stderr, x, ##__VA_ARGS__ ); }
#else
#	define LOG2( x, ... ) (void)0
#endif

typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long int ulong;

#define MAX_PACKETS_IN_QUEUE 256

#if NET_DEBUG >= 2
#	define log_net_debug2( x, ... ) do { fprintf(stderr, "log_net_debug2: "); fprintf( stderr, x, ##__VA_ARGS__ ); }while(false)
#else
#	define log_net_debug2( x, ... ) (void)0
#endif
#if NET_DEBUG >= 1
#	define log_net_debug( x, ... )  do { fprintf(stderr, "log_net_debug:  "); fprintf( stderr, x, ##__VA_ARGS__ ); }while(false)
#else
#	define log_net_debug( x, ... ) (void)0
#endif
